package com.xudow.app.ui.task.user;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.activeshare.edu.ucenter.common.messages.user.FavoriteListMessage;
import com.activeshare.edu.ucenter.models.user.UserFavoriteItemsWithOther;
import com.google.gson.Gson;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.newui.bean.FavoriteListMessageLS;
import com.xudow.app.util.FileUtils;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 我的收藏Task
 *
 * @author lukecheng
 */
public class FavoriteListTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(FavoriteListTask.class);

    private static final String FAVORITE_LIST_URL = Config.BASE_URL + Config.FAVORITE_LIST_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public FavoriteListTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(List<FavoriteListMessageLS.FavoriteListEntity> dataList) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        FavoriteListMessageLS.FavoriteListEntity[] dataArr = new FavoriteListMessageLS.FavoriteListEntity[dataList.size()];
        for (int i = 0; i < dataList.size(); i++) {
            dataArr[i] = dataList.get(i);
        }

        Bundle data = new Bundle();
        data.putSerializable("favorites", dataArr);
        message.setData(data);

        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
        XApplication application = (XApplication) context.getApplicationContext();

        String url = String.format(FAVORITE_LIST_URL, parameters[0].get("type"));

       /* if (parameters[0].get(Config.PAGINATION_PARAM_NAME) != null) {
            String paginationParam = String.format("&%s=%s", Config.PAGINATION_PARAM_NAME, parameters[0].get(Config.PAGINATION_PARAM_NAME));
            url += paginationParam;
        }*/

       /* String offsetParam = String.format("&%s=%s", Config.PAGINATION_OFFSET_PARAM_NAME, Config.PAGINATION_OFFSET_DEFAULT_VALUE);
        url += offsetParam;*/
        Log.i("FavoriteListTask","url>>>"+url);
        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        //restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
        try {
            // FavoriteListMessage result = restTemplate.getForObject(url, FavoriteListMessage.class);
            String string = restTemplate.getForObject(url, String.class);
            FavoriteListMessageLS result=new Gson().fromJson(string,FavoriteListMessageLS.class);
            if (result != null) {

               /* Log.e("url:", url+"");
                FileUtils.writeFile(result,new File(context.getCacheDir().getAbsolutePath(),"favoritelist.txt"));*/
                if (result.getResult().equals("0")) {
                    sendSuccessMessage(result.getFavoriteList());
                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        } catch (Exception e) {
            LOGE(TAG, e.getMessage());

            sendErrorMessage(e.getMessage());
        }

        return null;
    }
}
